package com.brayan.webapp.model.general.i18n;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;

import com.brayan.webapp.model.RootEntity;
import com.brayan.webapp.model.general.country.Country;

/**
 * Entity implementation class for Entity: Locale
 * 
 */
@Entity
public class Locale extends RootEntity {

	private static final long serialVersionUID = -6891202481871689120L;

	public Locale() {
		super();
	}

	@Id
	@TableGenerator(initialValue = 1000, name = "LocaleTableGen")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "LocaleTableGen")
	private Long id;

	@ManyToOne
	private Language language;

	@ManyToOne(optional = true)
	private Country country;

	@Transient
	private String localeCode;

	/**
	 * mappedBy is used to set the bi-directional relationship, which enforces
	 * the cascade.
	 */
	@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, mappedBy = "locale")
	private List<Translation> translations;

	public List<Translation> getTranslations() {
		return translations;
	}

	public void setTranslations(List<Translation> translations) {
		this.translations = translations;
	}

	public Long getId() {
		return id;
	}

	public Language getLanguage() {
		return language;
	}

	public void setLanguage(Language language) {
		this.language = language;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public String getLocaleCode() {
		String localeCode = "";
		if (getLanguage() != null)
			localeCode = getLanguage().getLanguageCode();
		if (getCountry() != null)
			localeCode += "-" + getCountry().getCountryCode();
		setLocaleCode(localeCode);
		return localeCode;
	}
	
	public void setLocaleCode(String localeCode) {
		this.localeCode = localeCode;
	}

}
